package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.QuestionBank;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.service.QuestionBankService;
import com.jyy.prefabricated.utils.UserSessionUtil;
import com.jyy.prefabricated.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 题库
 *
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class QuestionBankController {

    @Autowired
    private QuestionBankService questionBankService;

    @Resource
    private UserUtils userUtils;

    /**
     * 新增题库
     *
     * @param questionBank
     * @return
     */
    @PostMapping("/question-bank")
    public AjaxResult insert(@Valid @RequestBody QuestionBank questionBank) {
        log.info("Rest request to insert questionBank:{}", questionBank);
        int id = questionBankService.insert(questionBank);
        QuestionBank result = questionBankService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新题库
     *
     * @param questionBank
     * @return
     */
    @PutMapping("/question-bank")
    public AjaxResult update(@Valid @RequestBody QuestionBank questionBank) {
        log.info("Rest request to update questionBank:{}", questionBank);
        questionBankService.update(questionBank);
        QuestionBank result = questionBankService.selectById(questionBank.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询题库详情
     *
     * @param id
     * @return
     */
    @GetMapping("/question-bank/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find questionBank by id:{}", id);
        QuestionBank result = questionBankService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除题库
     *
     * @param id
     * @return
     */
    @DeleteMapping("/question-bank/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        log.info("Rest request to delete questionBank by id:{}", id);
        int row = questionBankService.delete(id);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询题库
     *
     * @param name                   名称
     * @param type                   类型
     * @param categoryId             分类id
     * @param trainingOrganizationId 企业id(企业登录管理后台调用必传、官网调用不用)
     * @param pageable               分页信息
     * @return
     */
    @GetMapping("/question-bank")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer type, @RequestParam(required = false) Integer questionState, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String remark, @RequestParam(required = false) Integer trainingOrganizationId, Pageable pageable) {
        log.info("Rest request to find questionBank by name:{},type:{},categoryId:{}", name, type, categoryId);
        List<Integer> categoryIds = questionBankService.selectDeepCategoryIds(categoryId);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<QuestionBank> questionBanks = questionBankService.selectDeepByParams(categoryIds, name, type, questionState, remark, trainingOrganizationId);
        PageInfo<QuestionBank> result = new PageInfo<>(questionBanks);
        return AjaxResult.success(result);
    }

    /**
     * 查询题库不分页
     *
     * @param name       名称
     * @param type       类型
     * @param categoryId 分类id
     * @return
     */
    @GetMapping("/question-bank-list")
    public AjaxResult selectAll(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer type, @RequestParam(required = false) Integer questionState, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String remark, @RequestParam(required = false) Integer questionListType) {
        log.info("Rest request to find questionBank by name:{}type:{},categoryId:{}", name, type, categoryId);
        List<Integer> categoryIds = questionBankService.selectDeepCategoryIds(categoryId);
        SysUser user = null;
        Integer trainingOrganizationId = null;
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token)) {
            user = userUtils.getCurrentUser();
        }
        if (user != null) {
            if (user.getTrainingOrganization() != null) {
                trainingOrganizationId = user.getTrainingOrganization().getId();
            }
        }
        List<QuestionBank> result = new ArrayList<>();
        if (questionListType != null && questionListType == 2) {
            result = questionBankService.selectTrainingOrganizationQuestionBankList(trainingOrganizationId);

        } else {
            result = questionBankService.selectDeepByParams(categoryIds, name, type, questionState, remark, trainingOrganizationId);
        }
        return AjaxResult.success(result);
    }


    /**
     * 查询企业购买的公开题库列表不分页
     *
     * @return
     */
    @GetMapping("/trainingOrganization-question-bank")
    public AjaxResult selectTrainingOrganizationQuestionBankList() {
        SysUser user = null;
        Integer trainingOrganizationId = null;
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token)) {
            user = userUtils.getCurrentUser();
        }
        if (user != null) {
            if (user.getTrainingOrganization() != null) {
                trainingOrganizationId = user.getId();
            }
        }
        List<QuestionBank> result = questionBankService.selectTrainingOrganizationQuestionBankList(trainingOrganizationId);
        return AjaxResult.success(result);
    }


}
